﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using UCS.ArtificialInteligence.AI;
using UCS.ArtificialInteligence.CitiesProblem.Model;
using UCS.ArtificialInteligence.CitiesProblem.View.ViewsModels;

namespace UCS.ArtificialInteligence.CitiesProblem.View.Commands
{
    public class FindFirstPathCommand : ICommand
    {
        #region Private Members

        private CountryViewModel _viewModel;

        #endregion

        #region Events

        public event EventHandler CanExecuteChanged;

        #endregion

        #region Constructors

        public FindFirstPathCommand(CountryViewModel viewModel)
        {
            _viewModel = viewModel;

            _viewModel.PropertyChanged += (sender, e) =>
                {
                    if (CanExecuteChanged == null)
                        return;

                    if (e.PropertyName == "From" || e.PropertyName == "To")
                        CanExecuteChanged(this, new EventArgs());
                };
        }

        #endregion

        #region Public Methods

        public bool CanExecute(object parameter)
        {
            return _viewModel.From != null && _viewModel.To != null;
        }

        public void Execute(object parameter)
        {
            IAIList<ProblemStateBorder> list = _viewModel.ListType == ListType.Amplitude ? (IAIList<ProblemStateBorder>)new AmplitudeList<ProblemStateBorder>() : (IAIList<ProblemStateBorder>)new DepthList<ProblemStateBorder>();
            var problem = new UCS.ArtificialInteligence.CitiesProblem.Model.CitiesProblem(_viewModel.From, _viewModel.To);
            var state = new BlindSearch(problem).FindFirst(list);

            _viewModel.FirstPath = state;
        }

        #endregion
    }
}
